package com.zlq.cn.util;

import cn.hutool.core.util.RandomUtil;
import one.util.streamex.StreamEx;
import org.apache.commons.lang3.StringUtils;

import java.util.Arrays;
import java.util.List;

/**
 * 生成imei
 * 规则：长度32位，每4个字符一组，每组
 */
public class ImeiUtil {
    private static final int IMEI_LENGTH = 32;
    private static final List<String> GROUP_1 = Arrays.asList("A", "B", "C", "D", "1", "3");
    private static final List<String> GROUP_2 = Arrays.asList("E", "F", "4", "6");
    private static final List<String> GROUP_3 = Arrays.asList("G", "H", "I", "M", "8");
    private static final List<String> GROUP_4 = Arrays.asList("0", "2", "5", "N");
    private static final List<String> GROUP_5 = Arrays.asList("I", "M", "K", "0", "9", "5");
    private static final List<String> GROUP_6 = Arrays.asList("0", "9", "P", "Q", "I");
    private static final List<String> GROUP_7 = Arrays.asList("O", "P", "Q", "3", "2", "4");
    private static final List<String> GROUP_8 = Arrays.asList("5", "6", "A", "D", "E");

    public static String generateImei() {
        return StreamEx.of(RandomUtil.randomEleList(GROUP_1, 4).stream()).joining() +
                StreamEx.of(RandomUtil.randomEleList(GROUP_2, 4).stream()).joining() +
                StreamEx.of(RandomUtil.randomEleList(GROUP_3, 4).stream()).joining() +
                StreamEx.of(RandomUtil.randomEleList(GROUP_4, 4).stream()).joining() +
                StreamEx.of(RandomUtil.randomEleList(GROUP_5, 4).stream()).joining() +
                StreamEx.of(RandomUtil.randomEleList(GROUP_6, 4).stream()).joining() +
                StreamEx.of(RandomUtil.randomEleList(GROUP_7, 4).stream()).joining() +
                StreamEx.of(RandomUtil.randomEleList(GROUP_8, 4).stream()).joining();
    }

    public static boolean checkImei(String imei) {
        if (StringUtils.isEmpty(imei) || imei.length() != IMEI_LENGTH) {
            return false;
        }
        char[] chars = imei.toCharArray();
        boolean contains;
        for (int i = 0; i < chars.length; i++) {
            if (i < 4) {
                contains = GROUP_1.contains(String.valueOf(chars[i]));
            } else if (i < 8) {
                contains = GROUP_2.contains(String.valueOf(chars[i]));
            } else if (i < 12) {
                contains = GROUP_3.contains(String.valueOf(chars[i]));
            } else if (i < 16) {
                contains = GROUP_4.contains(String.valueOf(chars[i]));
            } else if (i < 20) {
                contains = GROUP_5.contains(String.valueOf(chars[i]));
            } else if (i < 24) {
                contains = GROUP_6.contains(String.valueOf(chars[i]));
            } else if (i < 28) {
                contains = GROUP_7.contains(String.valueOf(chars[i]));
            } else {
                contains = GROUP_8.contains(String.valueOf(chars[i]));
            }
            if (!contains) {
                return false;
            }
        }
        return true;
    }

}
